package
{
	import flash.text.TextFormat;
	import flash.text.TextField;

	public class ConsoleObject
	{
		public var CONSOLE_KEY:Number;
		public var CONSOLE_KEY_WAS_DOWN:Boolean;
		public var UP_KEY:Number;
		public var DN_KEY:Number;
		public var SCROLL_WAS_DOWN:Boolean;
		public var SCROLLSPEED:Number;
		public var SCROLLCOUNTER:Number;
		public var fontsize:Number;
		public var numLines:Number;
		public var maxLen:Number;
		public var tabs:Number;
		public var buffer:Array;
		public var txt:String;
		public var topLine:Number;
		public var botLine:Number;
		public var pos:Vector2;
		public var dimensions:Vector2;
		public var curpos:Vector2;
		public var mc:TextField;
		public var active:Boolean;
		public var Animate:Function;
		public var curLine:Number;

		public function ConsoleObject(x:Number, y:Number, w:Number, h:Number)
		{
			this.CONSOLE_KEY = 109;
			this.CONSOLE_KEY_WAS_DOWN = false;
			this.UP_KEY = 105;
			this.DN_KEY = 99;
			this.SCROLL_WAS_DOWN = false;
			this.SCROLLSPEED = 4;
			this.SCROLLCOUNTER = 0;
			this.fontsize = 12;
			this.numLines = 16;
			this.maxLen = 80;
			this.tabs = 0;
			this.buffer = new Array();
			this.buffer.push("\n====[N]====");
			this.txt = "";
			this.topLine = 0;
			this.botLine = 0;
			this.pos = new Vector2(44, 28);
			this.dimensions = new Vector2(w, h);
			this.curpos = new Vector2(x, y);
			var depth:int = Main.getInstance().gfx.GetNextDepth_Front();
			this.mc = new TextField();
			this.mc.name = "consoleTxtField";
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
			this.mc.width = w;
			this.mc.height = h;
			this.mc.multiline = true;
			this.mc.wordWrap = true;
			this.mc.selectable = false;
			this.mc.embedFonts = true;
			//
			var _local2:TextFormat = new TextFormat();
			_local2.color = 0;
			_local2.font = "n_uni05_53";
			_local2.size = 8;
			_local2.align = "left";
			// this.mc.setNewTextFormat(_local2);
			this.mc.setTextFormat(_local2);
			//
			Main.getInstance().addChildAtDepth(this.mc, depth);

			this.active = true;
		}

		public function AnimateIn():void
		{
			var _local2 = this.pos.x - this.curpos.y;
			if (Math.abs(_local2) < 20)
			{
				this.curpos.y = this.pos.y;
				this.Animate = null;
			}
			else
			{
				this.curpos.y = this.curpos.y + _local2 * 0.15;
			}
			this.mc.y = this.curpos.y;
		}

		public function AnimateOut():void
		{
			var _local2 = (-this.dimensions.y) - this.curpos.y;
			if (Math.abs(_local2) < 20)
			{
				this.curpos.y = -this.dimensions.y;
				this.Animate = null;
				this.mc.visible = false;
			}
			else
			{
				this.curpos.y = this.curpos.y + (_local2 * 0.15);
			}
			this.mc.y = this.curpos.y;
		}

		// 循环执行
		public function Update():void
		{
			this.Animate();
			if (KeyManager.isDown(this.CONSOLE_KEY))
			{
				if (!this.CONSOLE_KEY_WAS_DOWN)
				{
					this.CONSOLE_KEY_WAS_DOWN = true;
					this.Toggle();
				}
			}
			else
			{
				this.CONSOLE_KEY_WAS_DOWN = false;
			}
			if (this.active)
			{
				var _local2 = 0;
				if (KeyManager.isDown(this.UP_KEY))
				{
					_local2 = _local2 - 1;
				}
				else if (KeyManager.isDown(this.DN_KEY))
				{
					_local2 = _local2 + 1;
				}
				else
				{
					this.SCROLL_WAS_DOWN = false;
				}
				if (_local2 != 0)
				{
					if (!this.SCROLL_WAS_DOWN)
					{
						this.SCROLL_WAS_DOWN = true;
						this.SCROLLCOUNTER = 0;
						this.topLine = Math.max(0, Math.min(this.buffer.length - this.numLines, this.topLine + _local2));
					}
					else if (this.SCROLLSPEED < (this.SCROLLCOUNTER++))
					{
						this.SCROLL_WAS_DOWN = false;
					}
				}
				this.Refresh();
			}
		}

		public function Show():void
		{
			this.mc.visible = true;
			this.active = true;
			this.Animate = this.AnimateIn;
		}

		public function Hide():void
		{
			this.active = false;
			this.Animate = this.AnimateOut;
		}

		public function Toggle():void
		{
			if (this.active)
			{
				this.Hide();
			}
			else
			{
				this.Show();
			}
		}

		public function Refresh():void
		{
			this.txt = "";
			var _local1 = this.topLine;
			while (_local1 < this.buffer.length)
			{
				this.txt = this.txt + this.buffer[_local1];
				_local1++;
			}
			this.mc.text = this.txt;
		}

		public function Clear():void
		{
			this.buffer = null;
			this.buffer = new Array();
			this.txt = "====[N]====";
			this.mc.text = this.txt;
			this.tabs = 0;
			this.curLine = 0;
		}

		public function AddLine(str):void
		{
			var _local2 = str;
			var _local1 = 0;
			while (_local1 < this.tabs)
			{
				_local2 = "\t" + _local2;
				_local1++;
			}
			_local2 = "\n" + _local2;
			this.botLine++;
			this.topLine = Math.max(this.topLine, this.botLine - this.numLines);
			this.buffer.pop();
			this.buffer.push(_local2);
			this.buffer.push("\n====[N]====");
		}

		public function Append(str):void
		{
			var _local2 = str;
			if ((this.maxLen - (this.tabs * 4)) < (this.buffer[this.buffer.length - 2].length + _local2.length))
			{
				this.AddLine(_local2);
			}
			else
			{
				this.buffer[this.buffer.length - 2] = this.buffer[this.buffer.length - 2] + _local2;
			}
		}

		public function StartTab():void
		{
			this.tabs++;
		}

		public function StopTab():void
		{
			this.tabs = Math.max(0, this.tabs - 1);
		}
	}
}